package mysqlpkg

import (
	"gitee.com/wanjimao/dongli-kit/pkg/setting"
	"go.uber.org/zap"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

type defaultMysqlDb struct {
	logger    *zap.Logger
	EnvServer string
}

// func NewMysqlDb(l *zap.Logger, set setting.Param, cService *curltools.CurlService) (*defaultMysqlDb, error) {
func NewMysqlDb(l *zap.Logger, set setting.Param) (*defaultMysqlDb, error) {
	return &defaultMysqlDb{
		logger:    l,
		EnvServer: set.EnvServer,
	}, nil
}

func (d defaultMysqlDb) MysqlGenerateGorm(dataSourceName string, tablePrefix string) (*gorm.DB, error) {
	//启动Gorm支持
	db, err := gorm.Open(mysql.Open(dataSourceName), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   tablePrefix, // 表名前缀，`User` 的表名应该是 `t_users`
			SingularTable: true,        // 使用单数表名，启用该选项，此时，`User` 的表名应该是 `t_user`
		},
		//开启sql日志
		Logger: logger.Default.LogMode(logger.Info),
	})
	return db, err
}

func (d defaultMysqlDb) UpdateModelDanceDb(name string, db *gorm.DB) {
	//启动Gorm支持
	switch name {
	case "1.0":
		//if err := db.AutoMigrate(&dancetiktokmodelmysql.DanceUserAccount{}); nil != err {
		//	panic(err)
		//}
		//if err := db.AutoMigrate(&dancetiktokmodelmysql.DanceUserAccountRole{}); nil != err {
		//	panic(err)
		//}
		//if err := db.AutoMigrate(&dancetiktokmodelmysql.DanceRandName{}); nil != err {
		//	panic(err)
		//} else {
		//}

		//if err := db.AutoMigrate(&modeldancespeedy.DanceSuit{}); nil != err {
		//	panic(err)
		//}
		//if err := db.AutoMigrate(&modeldancespeedy.DanceSuitElement{}); nil != err {
		//	panic(err)
		//}
		//if err := db.AutoMigrate(&modeldancespeedy.DanceUser{}); nil != err {
		//	panic(err)
		//}
		//if err := db.AutoMigrate(&modeldancespeedy.DanceUserBank{}); nil != err {
		//	panic(err)
		//}
		//if err := db.AutoMigrate(&modeldancespeedy.DanceProblem{}); nil != err {
		//	panic(err)
		//}
	}
}
